home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Sample Code Update 01⁄96 / MenuScripter 3.1 / Sources / MSMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-20  |  19.2 KB  |  801 lines  |  [TEXT/MPS ]

  1. /*
  2.     MSMain.c
  3.     
  4.     Version 3.1
  5.     
  6.     Copyright © 1995 Apple Computer, Inc., all rights reserved.
  7.     
  8.     MenuScripter by Nigel Humphreys and Jon Lansdell
  9.     AppleEvent to script extensions by Greg Sutton
  10.  
  11.     Changes from 3.0d9
  12.     
  13.     - Full native PPC support. 3.0d9 was missing some UPPs.
  14.     
  15.     - Added support of the 'contents' property for the selection. 'get the
  16.         contents of the selection of window 1' will now return the text of the
  17.         selection.
  18.     
  19.     - Removed Publish and Subscribe support, since it was buggy and has never
  20.         been properly fixed.
  21.         
  22.     - Updated code for connecting to the Scripting component.
  23. */
  24.  
  25. #include <Memory.h>
  26. #include <Quickdraw.h>
  27. #include <Types.h>
  28. #include <Menus.h>
  29. #include <Windows.h>
  30. #include <Dialogs.h>
  31. #include <Traps.h>
  32. #include <Packages.h>
  33. #include <PPCToolBox.h>
  34. #include <Printing.h>
  35. #include <ToolUtils.h>
  36. #include <Desk.h>
  37. #include <Scrap.h>
  38. #include <OSEvents.h>
  39. #include <AppleEvents.h>
  40. #include <AEObjects.h>
  41. #include <Errors.h>
  42. #include <PLStringFuncs.h>
  43.  
  44. #include "MSGlobals.h"
  45. #include "MSUtils.h"
  46. #include "MSAppleEvents.h"
  47. #include "MSWindow.h"
  48. #include "MSFile.h"
  49. #include "MSScript.h"
  50. #include "MSAEScript.h"
  51.  
  52. /*-----------------------------------------------------------------------*/
  53. /**----------                         Standard Main routines                  --------------**/
  54. /*-----------------------------------------------------------------------*/
  55.  
  56. #pragma segment Main
  57.  
  58. DialogPtr PoseWaitDialog()
  59.  
  60. {
  61.  DialogPtr progressDialog;
  62.  
  63.   /* pose a dialog indicating that we are connecting to the AppleScript component */
  64.     progressDialog = GetNewDialog(1006, nil, (WindowPtr)-1);
  65.     SetPort(progressDialog);
  66.     DrawDialog(progressDialog);
  67.     return progressDialog;
  68.     
  69. }
  70.  
  71.  
  72. #pragma segment Main
  73.  
  74.     pascal void MaintainCursor(void)
  75.         {
  76.             Point     pt;
  77.             WindowPtr wPtr;
  78.             GrafPtr   savePort;
  79.             DPtr      theDoc;
  80.  
  81.             wPtr = FrontWindow();
  82.             if (Ours(wPtr))
  83.                 {
  84.                     theDoc = DPtrFromWindowPtr(wPtr);
  85.                     GetPort(&savePort);
  86.                     SetPort(wPtr);
  87.                     GetMouse(&pt);
  88.                     if (theDoc->theText)
  89.                         if (PtInRect(pt, &(**(theDoc->theText)).viewRect))
  90.                             SetCursor(&editCursor);
  91.                         else
  92.                             SetCursor(&qd.arrow);
  93.                     else
  94.                         SetCursor(&qd.arrow);
  95.  
  96.                     if (theDoc->theText)
  97.                         TEIdle(theDoc->theText);
  98.  
  99.                     SetPort(savePort);
  100.                 }
  101.         }
  102.  
  103. #pragma segment Main
  104.  
  105. pascal void MaintainMenus(void)
  106. {
  107.     DPtr       theDoc;
  108.     WindowPtr  firstWindow;
  109.  
  110.     firstWindow = FrontWindow();
  111.     if (!Ours(firstWindow))
  112.                     {
  113.                                     EnableItem(myMenus[fileM], fmNew);
  114.                                     EnableItem(myMenus[fileM], fmOpen);
  115.                                     DisableItem(myMenus[fileM], fmClose);
  116.                                     DisableItem(myMenus[fileM], fmSave);
  117.                                     DisableItem(myMenus[fileM], fmSaveAs);
  118.                                     DisableItem(myMenus[fileM], fmRevert);
  119.                                     DisableItem(myMenus[fileM], fmPrint);
  120.                                     DisableItem(myMenus[fileM], fmPageSetUp);
  121.                                     DisableItem(myMenus[scriptM], cCompile);
  122.                                     DisableItem(myMenus[scriptM], cExecute);
  123.  
  124.                                     EnableAEScriptItems(false);
  125.  
  126.                                     if (firstWindow)
  127.                                                     {
  128.                                                         EnableItem(myMenus[editM],undoCommand);
  129.                                                         EnableItem(myMenus[editM],cutCommand);
  130.                                                         EnableItem(myMenus[editM],copyCommand);
  131.                                                         EnableItem(myMenus[editM],pasteCommand);
  132.                                                         EnableItem(myMenus[editM],clearCommand);
  133.                                                     }
  134.                     }
  135.     else
  136.                     {
  137.                             theDoc = DPtrFromWindowPtr(firstWindow);
  138.                             EnableItem(myMenus[editM], pasteCommand);
  139.                             EnableItem(myMenus[fileM], fmClose);
  140.                             EnableItem(myMenus[fileM], fmSaveAs);
  141.                             EnableItem(myMenus[fileM], fmPrint);
  142.                             EnableItem(myMenus[fileM], fmPageSetUp);
  143.  
  144.                             if (theDoc->dirty)
  145.                                             EnableItem(myMenus[fileM], fmRevert);
  146.                             else
  147.                                             DisableItem(myMenus[fileM], fmRevert);
  148.  
  149.                             if ((theDoc->dirty) && (theDoc->everSaved))
  150.                                             EnableItem(myMenus[fileM], fmSave);
  151.                             else
  152.                                             DisableItem(myMenus[fileM], fmSave);
  153.  
  154.                             DisableItem(myMenus[editM], undoCommand);
  155.  
  156.                             if (((**(theDoc->theText)).selEnd - (**(theDoc->theText)).selStart) < 0)
  157.                                                 {
  158.                                                     DisableItem(myMenus[editM],cutCommand);
  159.                                                     DisableItem(myMenus[editM],copyCommand);
  160.                                                     DisableItem(myMenus[editM],clearCommand);
  161.                                                 }
  162.                                 else
  163.                                                 {
  164.                                                     EnableItem(myMenus[editM],cutCommand);
  165.                                                     EnableItem(myMenus[editM],copyCommand);
  166.                                                     EnableItem(myMenus[editM],clearCommand);
  167.                                                 }
  168.  
  169.                                     EnableItem(myMenus[scriptM], cCompile);
  170.                                     EnableItem(myMenus[scriptM], cExecute);
  171.  
  172.                                     EnableAEScriptItems(true);
  173.                                 }
  174. }
  175.  
  176. #pragma segment Main
  177.  
  178. pascal void SetUpCursors(void)
  179.  
  180.     {
  181.         CursHandle  hCurs;
  182.  
  183.         hCurs = GetCursor(1);
  184.         editCursor = **hCurs;
  185.         hCurs = GetCursor(watchCursor);
  186.         waitCursor = **hCurs;
  187.     }
  188.  
  189. #pragma segment Main
  190.  
  191. pascal void SetUpMenus(void)
  192.     {
  193.         short i;
  194.         
  195.         myMenus[appleM] = GetMenu(appleID);
  196.         AddResMenu(myMenus[appleM], 'DRVR');
  197.         myMenus[fileM] = GetMenu(fileID);
  198.         myMenus[editM] = GetMenu(editID);
  199.         myMenus[fontM] = GetMenu(mfontID);
  200.         AddResMenu(myMenus[fontM], 'FONT');
  201.         myMenus[sizeM]  = GetMenu(sizeID);
  202.         myMenus[styleM] = GetMenu(styleID);
  203.         myMenus[scriptM] = GetMenu(mscriptID);
  204.  
  205.         for (i = appleM; i <= kLastMenu; i++)
  206.         {
  207.             InsertMenu(myMenus[i], 0);
  208.         }
  209.  
  210.         SetItemStyle(myMenus[styleM], cPlain, 0);
  211.         SetItemStyle(myMenus[styleM], cBold, bold);
  212.         SetItemStyle(myMenus[styleM], cItalic, italic);
  213.         SetItemStyle(myMenus[styleM], cUnderline, underline);
  214.         SetItemStyle(myMenus[styleM], cOutline, outline);
  215.         SetItemStyle(myMenus[styleM], cShadow, shadow);
  216.         SetItemStyle(myMenus[styleM], cCondense, condense);
  217.         SetItemStyle(myMenus[styleM], cExtend, extend);
  218.  
  219.         SetShortMenus(); /* Does a DrawMenuBar() */
  220.     }
  221.     
  222.  
  223. #pragma segment Main
  224.  
  225. // IdleProc for AESend 
  226. pascal Boolean IdleProc(EventRecord *myEvent, long *sleep, RgnHandle *mouseRgn)
  227. {
  228.         DPtr        theDoc;
  229.         WindowPtr   theWindow;
  230.         Boolean     activate;
  231.  
  232.     switch (myEvent->what)
  233.     {
  234.         case nullEvent:
  235.             *sleep = 0;                         // no null processing in this sample
  236.             mouseRgn = nil;
  237.             break;
  238.                         
  239.                 case activateEvt:
  240.                         activate = ((myEvent->modifiers & activeFlag) != 0);
  241.                         theWindow = (WindowPtr)myEvent->message;
  242.                         DoActivate(theWindow, activate);
  243.                       break;
  244.  
  245.                 case updateEvt:
  246.                         theDoc = DPtrFromWindowPtr((WindowPtr)myEvent->message);
  247.                         DoUpdate(theDoc);
  248.                       break;
  249.  
  250.                 case kOSEvent:
  251.                     switch (myEvent->message & osEvtMessageMask)             // High byte of message
  252.                     {
  253.                         case 0x01000000:
  254.                             {
  255.                                 gInBackground = ((myEvent->message & resumeFlag) == 0);
  256.                                 DoActivate(FrontWindow(), ! gInBackground);
  257.                             }
  258.                             break;
  259.  
  260.                         default:
  261.                             SysBeep(1);
  262.                             break;
  263.                     }
  264.                     break;
  265.                     
  266.                 default:
  267.                     SysBeep(1);
  268.                     break;
  269.     }
  270.     
  271.         return(false);    // I'll wait forever
  272. }
  273.  
  274.  
  275. #pragma segment Main
  276.  
  277.  
  278. pascal void ScrollVActionProc(ControlHandle control, short part);
  279. pascal void VActionProc(ControlHandle control, short part);
  280. pascal void HActionProc(ControlHandle control, short part);
  281. pascal void DrawStyledTextEditRec(DialogPtr theDialog, short theItem);
  282. pascal void DrawScrollBar(DialogPtr theDialog, short theItem);
  283.  
  284.  
  285. pascal void SetUpUniversalProcedures(void)
  286.     {
  287.         gScrollDocVActionUPP = NewControlActionProc(VActionProc);
  288.         gScrollDocHActionUPP = NewControlActionProc(HActionProc);
  289.  
  290.         gScrollScriptVActionUPP = NewControlActionProc(ScrollVActionProc);
  291.         gDrawStyledTextUPP = NewControlActionProc(DrawStyledTextEditRec);
  292.         gDrawScrollBarUPP = NewControlActionProc(DrawScrollBar);
  293.         
  294.         gAEIdleUPP = NewAEIdleProc(IdleProc);
  295.     }
  296.  
  297.  
  298. pascal void DoFile(short theItem)
  299. {        
  300.         short   alertResult;
  301.         DPtr    theDoc;
  302.         FSSpec  theFSSpec;
  303.         OSErr   fileErr;
  304.         TPrint  thePSetup;
  305.         Str255  revertName;
  306.  
  307.         switch (theItem){
  308.             case fmNew : IssueAENewWindow();
  309.                                      break;
  310.             case fmOpen: if (GetFile(&theFSSpec)==noErr)
  311.                                          fileErr = IssueAEOpenDoc(theFSSpec);
  312.                                      break;
  313.             case fmClose:IssueCloseCommand(FrontWindow());
  314.                                      break;
  315.             case fmSave:
  316.             case fmSaveAs:
  317.                                         theDoc = DPtrFromWindowPtr(FrontWindow());
  318.                     
  319.                                         if (theDoc->everSaved == false || theItem == fmSaveAs)
  320.                                             {
  321.                                                 fileErr = GetFileNameToSaveAs(theDoc);
  322.                                                 if (fileErr == userCanceledErr)
  323.                                                     break;
  324.                                                 else if (fileErr != noErr)
  325.                                                     FileError((unsigned char *)"\perror saving ", theDoc->theFileName);
  326.                                                 else
  327.                                                     fileErr = IssueSaveCommand(theDoc->theWindow, &theDoc->theFSSpec);
  328.                     
  329.                                                 if (fileErr == noErr)
  330.                                                 {
  331.                                                     SetWTitle(theDoc->theWindow, theDoc->theFSSpec.name);
  332.                                                     theDoc->everSaved = true;
  333.                                                 }
  334.                                             }
  335.                                         else
  336.                                             fileErr = IssueSaveCommand(theDoc->theWindow, nil);
  337.                                         break;
  338.     
  339.             case fmRevert:SetCursor(&qd.arrow);
  340.                                         theDoc = DPtrFromWindowPtr(FrontWindow());
  341.                                         
  342.                                         if (theDoc->everSaved)
  343.                                             PLstrcpy(revertName,theDoc->theFileName);
  344.                                         else
  345.                                             GetWTitle(theDoc->theWindow, revertName);
  346.                                             
  347.                                         ParamText((unsigned char *)"\pRevert to the last saved version of", 
  348.                                              revertName, (unsigned char *)"", (unsigned char *)"");
  349.                                         alertResult = Alert(AdviseAlert, nil);
  350.                                         switch (alertResult){
  351.                                             case aaSave:if (IssueRevertCommand(theDoc->theWindow))
  352.                                                                         FileError((unsigned char *)"\perror reverting ",
  353.                                                                         theDoc->theFileName);
  354.                                         }
  355.                                         break;
  356.  
  357.             case fmPageSetUp:
  358.                                              theDoc = DPtrFromWindowPtr(FrontWindow());
  359.                                              if (DoPageSetup(theDoc))
  360.                                                  {
  361.                                                      thePSetup = **(theDoc->thePrintSetup);
  362.                                                      IssuePageSetupWindow(theDoc->theWindow, thePSetup);
  363.                                                  }
  364.                                              break;
  365.  
  366.             case fmPrint: IssuePrintWindow(FrontWindow());
  367.                                         break;
  368.  
  369.             case fmQuit : IssueQuitCommand();
  370.                                         break;
  371.         }                 /*of switch*/
  372. }
  373.  
  374.  
  375.  
  376.  
  377. #pragma segment Main
  378.  
  379. pascal void DoCommand(long mResult)
  380. {
  381.         short   theItem;
  382.         short   theMenuID;
  383.         short   err;
  384.         long    result;
  385.         Str255  name;
  386.         DPtr    theDocument;
  387.         Boolean exists;
  388.         Boolean useOldCode;
  389.         Boolean wasDirty;
  390.  
  391.         if (!gInBackground) HiliteMenu(0);
  392.  
  393.         theDocument = DPtrFromWindowPtr(FrontWindow());
  394.  
  395.         theItem   = LoWord(mResult);
  396.         theMenuID = HiWord(mResult);
  397.  
  398.         err = ScriptForMenuExists(theMenuID, theItem, &exists);
  399.  
  400.         if (err==noErr && exists==true)
  401.                         useOldCode = (ExecuteScriptForMenu(theMenuID,theItem)!=noErr);
  402.         else
  403.                         useOldCode = true;
  404.  
  405.         if (useOldCode)
  406.                         switch (theMenuID){
  407.  
  408.                         case appleID:
  409.                                     if (theItem == aboutItem)
  410.                                         {
  411.                                             SetCursor(&qd.arrow);
  412.                                             result = Alert(258,nil);
  413.                                         }
  414.                                      else
  415.                                      {
  416.                                        GetItem(myMenus[appleM], theItem, name);
  417.                                          err = OpenDeskAcc(name);
  418.                                          SetPort(FrontWindow());
  419.                                         }
  420.                                      break;
  421.  
  422.                          case fileID: DoFile(theItem);
  423.                                             break;
  424.  
  425.                          case editID:
  426.                              if (SystemEdit(theItem - 1) == false)
  427.                                   {
  428.                                     }
  429.                                  switch (theItem){
  430.                                    case cutCommand   :IssueCutCommand(theDocument);
  431.                    break;
  432.  
  433.                                      case copyCommand  : IssueCopyCommand(theDocument);
  434.                                      break;
  435.  
  436.                                      case pasteCommand : IssuePasteCommand(theDocument);
  437.                                      break;
  438.  
  439.                    case clearCommand : IssueClearCommand(theDocument);
  440.                                      break;
  441.  
  442.                                      case selectAllCommand :
  443.                                       if (theDocument) TESetSelect(0, (**(theDocument->theText)).teLength,
  444.                                                                                       theDocument->theText);
  445.                                         break;
  446.                                 }        /*of switch*/
  447.                             ShowSelect(theDocument);
  448.                             break;
  449.  
  450.                 case mfontID: IssueFontCommand(theDocument,theItem);
  451.         break;
  452.  
  453.                 case sizeID: IssueSizeCommand(theDocument,theItem);
  454.                 break;
  455.  
  456.                 case styleID: IssueStyleCommand(theDocument, theItem);
  457.                 break;
  458.  
  459.                 case mscriptID: switch (theItem){
  460.                  
  461.                   case cCompile  : CompileDocument(theDocument);
  462.                     break;
  463.  
  464.                     case cExecute  : ExecuteDocument(theDocument);
  465.                     break;
  466.  
  467.                     case cScript1    : ExecuteScript1(theDocument);
  468.                     break;
  469.  
  470.                     case cScript2  : ExecuteScript2(theDocument);
  471.                     break;
  472.                     
  473.                     case cScript3  : ExecuteScript3(theDocument);
  474.                     break;
  475.                     
  476.                     case cScript4  : wasDirty = theDocument->dirty;
  477.                         theDocument->dirty = true;
  478.                         DoFile(fmSaveAs);
  479.                         
  480.                         if (! theDocument->dirty)
  481.                         ExecuteScript4(theDocument);
  482.     
  483.                         else
  484.                         theDocument->dirty = wasDirty;
  485.                         break;
  486.                     
  487.                     }
  488.                     break;
  489.  
  490.         }   /*of switch*/
  491.  
  492. } /* DoCommand */
  493.  
  494.  
  495. #pragma segment Main
  496.  
  497. pascal void DoMouseDown(const EventRecord *myEvent)
  498.     {
  499.         WindowPtr whichWindow;
  500.         Point     p;
  501.         Rect      dragRect;
  502.         Rect      oldPosn;
  503.         DPtr      theDoc;
  504.         long      menuResult;
  505.         OSErr     myErr;
  506.         Boolean   scriptExists;
  507.         WindowPtr oldFront;
  508.  
  509.         p = myEvent->where;
  510.         switch (FindWindow(p, &whichWindow)){
  511.  
  512.             case inDesk: SysBeep(10);
  513.                                      break;
  514.  
  515.             case inGoAway:if (Ours(whichWindow))
  516.                                             if (TrackGoAway(whichWindow, p))
  517.                                                 IssueCloseCommand(whichWindow);
  518.                                         break;
  519.             case inMenuBar:
  520.                                         SetCursor(&qd.arrow);
  521.                                         theDoc = DPtrFromWindowPtr(FrontWindow());
  522.                                         if (theDoc)
  523.                                             {
  524.                                                 SetFontMenu(theDoc);
  525.                                             }
  526.                                         
  527.                                         menuResult = MenuSelect(p);
  528.                                         
  529.                                         /*
  530.                                             Check for script editing -
  531.                                           ctrl+option when selecting the menu = Edit Script )if any)
  532.                                         */
  533.                                         
  534.                                         if (OptionKeyPressed(myEvent) && CtrlKeyPressed(myEvent))
  535.                                             {
  536.                                                 myErr = ScriptForMenuExists(HiWord(menuResult), LoWord(menuResult), &scriptExists);
  537.                                                 if (scriptExists && myErr==noErr)
  538.                                                     EditMenuScript(HiWord(menuResult), LoWord(menuResult));
  539.                                                 else
  540.                                                     SysBeep(10);
  541.                                             }
  542.                                         else
  543.                                             DoCommand(menuResult);
  544.                                             
  545.                                         HiliteMenu(0);
  546.                                             
  547.                                         break;
  548.  
  549.             case inSysWindow: SystemClick(myEvent, whichWindow);
  550.                                                 break;
  551.  
  552.             case inDrag:
  553.                 
  554.                     dragRect = qd.screenBits.bounds;
  555.                     
  556.                     if (Ours(whichWindow))
  557.                         {
  558.                             oldFront = FrontWindow();
  559.                             oldPosn  = (**((WindowPeek)whichWindow)->strucRgn).rgnBBox;
  560.                             
  561.                             DragWindow(whichWindow, p, &dragRect);
  562.                             
  563.                             if ((whichWindow==FrontWindow()) &&
  564.                                 (whichWindow!=oldFront))
  565.                                 IssueSelectWindowCommand(whichWindow, oldFront); // Record the select part of drag
  566.                             /*
  567.                                 As rgnBBox may be passed by address
  568.                             */
  569.                             dragRect = (**((WindowPeek)whichWindow)->strucRgn).rgnBBox;
  570.                             /*
  571.                                 The windows already there, but still tell
  572.                                 the our AppleEvents core about the move in case
  573.                                 they want to do anything - or record
  574.                             */
  575.                             if (EqualRect(&dragRect, &oldPosn)==false)
  576.                                 IssueMoveWindow(whichWindow, dragRect);
  577.                         }
  578.                     break;
  579.  
  580.             case inGrow:SetCursor(&qd.arrow);
  581.                                     if (Ours(whichWindow))
  582.                                         MyGrowWindow(whichWindow, p);
  583.                                     break;
  584.                                 
  585.             case inZoomIn : DoZoom(whichWindow, inZoomIn, p);
  586.                                             break;
  587.  
  588.             case inZoomOut: DoZoom(whichWindow, inZoomOut, p);
  589.                                             break;
  590.             case inContent: if (whichWindow != FrontWindow())
  591.                                                 {
  592.                                                     IssueSelectWindowCommand(whichWindow, FrontWindow()); // Record the selectWindow
  593.                                                     SelectWindow(whichWindow);
  594.                                                 }
  595.                                             else
  596.                                                 if (Ours(whichWindow))
  597.                                                     DoContent(whichWindow, *myEvent);
  598.                                             break;
  599.         }                 /*of switch*/
  600.     }
  601.  
  602. #pragma segment Main
  603.  
  604. pascal long GetSleep(void)
  605.     {
  606.         long      sleep;
  607.         WindowPtr theWindow;
  608.         DPtr      theDoc;
  609.  
  610.         sleep = 0x7fffffff;
  611.         if (!gInBackground)
  612.             {
  613.                 theWindow = FrontWindow();
  614.                 if (theWindow)
  615.                     {
  616.                         theDoc = DPtrFromWindowPtr(theWindow);
  617.                         if ((**(theDoc->theText)).selStart == (**(theDoc->theText)).selEnd)
  618.                             sleep = GetCaretTime();
  619.                     }
  620.             }
  621.         return(sleep);
  622.     }                     /*GetSleep*/
  623.  
  624. #pragma segment Main
  625.  
  626.     
  627. pascal void MainEvent(void)
  628.     {
  629.         DPtr        theDoc;
  630.         EventRecord myEvent;
  631.         char        theChar;
  632.         WindowPtr   theWindow;
  633.         Boolean     activate;
  634.         
  635.  
  636.         MaintainCursor(); /* TEIdle in here for now */
  637.         MaintainMenus();
  638.  
  639.         if (WaitNextEvent(everyEvent, &myEvent, GetSleep(), nil))
  640.             {
  641.                 switch (myEvent.what) {
  642.                     case mouseDown: FlushAndRecordTypingBuffer();
  643.                                                     DoMouseDown(&myEvent);
  644.                                     break;
  645.                     case keyDown:
  646.                     case autoKey:
  647.                                 theDoc = DPtrFromWindowPtr(FrontWindow());
  648.     
  649.                                 theChar = myEvent.message & charCodeMask;
  650.                   
  651.                                 if (theChar==3) // Enter key == compile
  652.                                     {
  653.                                         CompileDocument(theDoc);
  654.                                     }
  655.                                 else
  656.                                 if ((myEvent.modifiers & cmdKey) == cmdKey)
  657.                                     {
  658.                                         DoCommand(MenuKey(theChar));
  659.                                     }
  660.  
  661.                                 else
  662.                                     if (theDoc->theText)
  663.                                         {
  664.                                             AddKeyToTypingBuffer(theDoc, theChar);
  665.                                             
  666.                                             TEKey(theChar, theDoc->theText);
  667.                                             
  668.                                             AdjustScrollbars(theDoc, false);
  669.                                             ShowSelect(theDoc);
  670.                                             
  671.                                             theDoc->dirty = true;
  672.                                         }
  673.                               break;
  674.                         
  675.                     case activateEvt:
  676.                             activate = ((myEvent.modifiers & activeFlag) != 0);
  677.                             theWindow = (WindowPtr)myEvent.message;
  678.                             DoActivate(theWindow, activate);
  679.                           break;
  680.  
  681.                     case updateEvt:
  682.                             theDoc = DPtrFromWindowPtr((WindowPtr)myEvent.message);
  683.                             DoUpdate(theDoc);
  684.                           break;
  685.  
  686.                     case kHighLevelEvent: FlushAndRecordTypingBuffer();
  687.                                                                 DoAppleEvent(myEvent);
  688.                                 break;
  689.                     case kOSEvent:
  690.                         
  691.                         switch (myEvent.message & osEvtMessageMask) { /*high byte of message*/
  692.                             case 0x01000000:
  693.                                 {
  694.                                     gInBackground = ((myEvent.message & resumeFlag) == 0);
  695.                                     DoActivate(FrontWindow(), !gInBackground);
  696.                                 }
  697.                         }
  698.                     }
  699.                 }             /*of switch*/
  700.         }
  701.  
  702.  
  703. #pragma segment Main
  704.  
  705. pascal void DoSVEdit(void)
  706.     {
  707.         OSErr     err;
  708.         short     result;
  709.         DialogPtr progressDialog;
  710.         GrafPtr         oldPort;
  711.  
  712.         InitGraf(&qd.thePort);
  713.         InitFonts();
  714.         FlushEvents(everyEvent, 0);
  715.         InitWindows();
  716.         InitMenus();
  717.         TEInit();
  718.         InitDialogs(0L);
  719.         InitCursor();
  720.  
  721.         MaxApplZone();
  722.         SetUpCursors();
  723.  
  724.         SetUpMenus();
  725.         
  726.         SetUpUniversalProcedures();
  727.  
  728.         gWCount      = 0;
  729.         gNewDocCount = 0;
  730.         gQuitting    = false;
  731.         gFontMItem   = 0;
  732.  
  733.         gGestaltAvailable          = false;
  734.         gAppleEventsImplemented    = false;
  735.         gAliasManagerImplemented   = false;
  736.         gOutlineFontsImplemented   = false;
  737.  
  738.         /*check environment checks to see if we are running 7.0*/
  739.         if (!CheckEnvironment())
  740.             {
  741.                 SetCursor(&qd.arrow);
  742.                 /*pose the only 7.0 alert*/
  743.                 result = Alert(302, nil);
  744.                 return;
  745.             }
  746.             
  747.         GetVolumeAndDirectory();        // Call before SetUpScripts() - so files can be found
  748.  
  749.         err = AEObjectInit();
  750.         if (err)
  751.             {
  752.                 ShowError((unsigned char *)"\pAEObjectInit", err);
  753.                 gQuitting = true;
  754.             }
  755.  
  756.         InitAppleEvents();
  757.  
  758.         err = PPCInit();
  759.         if (err)
  760.             {
  761.                 ShowError((unsigned char *)"\pPPCInit", err);
  762.                 gQuitting = true;
  763.             }
  764.             
  765.         GetPort(&oldPort);
  766.         
  767.       progressDialog =  PoseWaitDialog();
  768.  
  769.     err = InitEditorScripting();
  770.         if (err)
  771.             {
  772.               DisposeDialog(progressDialog);
  773.               SetPort(oldPort);
  774.                 ShowError((unsigned char *)"\pInitEditorScripting", err);
  775.                 gQuitting = true;
  776.             }
  777.         else
  778.             {
  779.                 SetUpScripts();                                    // Need gScriptingComponent loaded so do after InitEditorScripting()
  780.              
  781.               DisposeDialog(progressDialog);
  782.               
  783.               SetPort(oldPort);
  784.                               
  785.               SetOSAActiveProcedure();                // Set after all of our scripts are loaded
  786.                                 
  787.                 while (!gQuitting)
  788.                     MainEvent();
  789.             
  790.                 err = CleanUpAEScripts();
  791.                 err = CloseEditorScripting();
  792.             }
  793.  
  794.     }
  795.     
  796. void main ()
  797.     {
  798.         /*the main routine starts here*/
  799.         DoSVEdit();
  800.     }
  801.